﻿$(document).ready(function() {

    //Limit character for Message
    $('#Content').charCounter(MessageContentLimit, {
        container: '<span></span>',
        classname: 'grey',
        format: '',
        pulse: false,
        delay: 1
    });

    $("#btnSend").click(function(evt) {
        var $group = $('#message-popup');
        var isValid = true;
        //alert('0');
        $group.find(':input[type!=button]').each(function(i, item) {
            if (!$(item).valid()) {
                if (isValid == true)
                    $(item).focus();
                isValid = false;
            }
        });
        //alert('1');
        if (!isValid)
            evt.preventDefault();
        else {
            if ($("#Subject").val() == "") {
                jAlert(SubjectMessageMissError);
                return;
            }

            if ($("#Subject").val().length > MessageTiteLimit) {
                jAlert(SubjectMessageLengthError);
                return;
            }

            if ($("#Content").val().length == "") {
                jAlert(ContentMissError);
                return;
            }

            if ($("#Content").val().length > MessageContentLimit) {
                jAlert(MessageLengthError);
                return;
            }

            $("#btnSend").hide();
            $("#btnClose").hide();
            $("#ajaxloader").show();
            $.ajax({
                async: false,
                dataType: 'json',
                url: "/api/user.ashx",
                type: 'POST',
                data: {
                    action: 'sendMessage',
                    messageTitle: $("#Subject").val(),
                    messageContent: $("#Content").val(),
                    receiverUserKey: $("#ReceiverUserKey").val()
                },
                success: function(data) {
                    if (data.result == "-1" || data.result == "1") {
                        window.parent.SendMessageComplete(data.result);
                        return;
                    }
                    $("#btnSend").show();
                    $("#btnClose").show();
                    $("#ajaxloader").hide();
                    if (data.result == "0") {
                        jAlert(SystemError);
                        return;
                    }
                    if (data.result == "2") {
                        jAlert(SubjectMessageMissError);
                        return;
                    }
                    if (data.result == "3") {
                        jAlert(ContentMissError);
                        return;
                    }
                    if (data.result == "4") {
                        jAlert(ReceiverKeyError);
                        return;
                    }
                    if (data.result == "5") {
                        jAlert(VarifiedAccountRequired);
                        return;
                    }
                    if (data.result == "6") {
                        jAlert(SendYourselfMessageError);
                        return;
                    }
                    if (data.result == "7") {
                        jAlert(SendMore3MessageError);
                        return;
                    }
                    if (data.result == "8") {
                        jAlert(ContentNotAppropriateError);
                        return;
                    }
                }
            });
        }
    });

    //Control Behavior
    $('input').focus(function() {
        $(this).addClass("itemfocus");
    });

    $('input').blur(function() {
        $(this).removeClass("itemfocus");
    });

    $('textarea').focus(function() {
        $(this).addClass("itemfocus");
    });
    $('textarea').blur(function() {
        $(this).removeClass("itemfocus");
    });

    //    //Define validate function
    //    jQuery.validator.addMethod("checkConfirmNewPassword", function(value, element) {
    //        if ($("#NewPassword").val() != $("#ConfirmNewPassword").val()) {
    //            return false;
    //        }
    //        return true;
    //    }, ReTypePassMissMatchError);

    //Define Rule For Form
    Valid = $("#formmessage").validate({
        errorPlacement: function(error, element) {
            $(element).attr("title", $(error).html());
        },
        errorClass: "field-error",
        errorElement: "div",
        unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass(errorClass).addClass(validClass);
        },
        rules: {
            Subject: {
                required: true,
                maxlength: MessageTiteLimit
            },
            Content: {
                required: true,
                maxlength: MessageContentLimit
            }
        },
        messages: {
            Subject: {
                required: SubjectMessageMissError,
                maxlength: MessageLengthError
            },
            Content: {
                required: ContentMissError,
                maxlength: MessageLengthError
            }
        }
    });
    //end: Validate For Form
});